package com.ymatou.iapi.productmanager.testcase;

import com.ymatou.iapi.productmanager.parameter.GetProductCatalogInfosBean;
import com.ymatou.iapi.productmanager.parameter.ProductCatalogDtoV2;
import com.ymatou.iapi.productmanager.parameter.ProductCatalogListBean;
import com.ymatou.iapi.productmanager.parameter.SaveGrouponBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productmanager.service.GetProductCatalogInfosCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ProductManagerCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.database.model.GrouponProducts;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.compare.AssertHelper;
import org.json.JSONObject;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 获取商品规格列表
 * @author yangyutong 2019年8月19日 下午5:19:23
 *
 */
public class Ts_GetProductCatalogInfos {
	private static GetProductCatalogInfosCall getProductCatalogListCall = new GetProductCatalogInfosCall();
    private static GetProductCatalogInfosBean getProductCatalogInfosBean = new GetProductCatalogInfosBean();
	private static Integer sellerId = Integer.valueOf(EnvSetup.getData("productsellerid"));
	private static String sellerName = EnvSetup.getData("productsellername");

	private static int sellerId2 = Integer.parseInt(EnvSetup.getData("globalSellerId"));

	//优选买手
	private static Integer sellerId1 = 3383;
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取商品规格列表");
//		//SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(sellerId);
	}
	
	@Before
	public void caseUp() {
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getProductCatalogListCall.getOpurl());
	}
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductCatalogList_001() {
		Logger.start(true,"获取商品规格列表-多规格商品");
		try{
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addProdReq);
			
			checkGetProductCatalogList(sellerId,productId);
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductCatalogList_002() {
		Logger.start(true,"获取商品规格列表-组合商品");
		try{
			
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			Integer pkgid=ProductManagerCallServiceV2.saveCatalogPackage("Pkg_"+TestDataManager.getRandomNumToHex(5));
			addProdReq.getProduct().getCatalogList().forEach(catalog->{
				catalog.setPackage(true);
				catalog.setSubCatalogIdList(Arrays.asList(pkgid));
			});
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addProdReq);;
			
			checkGetProductCatalogList(sellerId,productId);
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductCatalogList_003() {
		Logger.start(true,"获取商品规格列-非该商品买手无法查询该商品");
		try{
			
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addProdReq);;
			JSONObject ret = getProductCatalogListCall.call(sellerId+1, productId,0);
			Logger.verifyEquals(true, ret.isNull("Data"), "非该商品买手查询，结果应为空");
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductCatalogList_004() {
		Logger.start(true,"获取商品规格列-拼团商品规格,创建时");
		try{
			//创建一个拼团商品
			AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId1);
			setStock(addpro, 30);
			setPrice(addpro, 100d);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			SaveGrouponBean saveGrouponBean = ProductManagerCallServiceV3.initSaveGrouponBean(sellerId1,productId,80d);
            saveGrouponBean.setGrouponMemberCount(12);
			ProductManagerCallServiceV3.SaveGrouponCall(saveGrouponBean);

            getProductCatalogInfosBean.setProductId(productId);
            getProductCatalogInfosBean.setUserId(sellerId1);
            getProductCatalogInfosBean.setListType(1);
            getProductCatalogListCall.setData(getProductCatalogInfosBean);
            getProductCatalogListCall.callService();

            checkGetProductCatalogList(sellerId1,productId);
        }catch(Exception e){
			Logger.fail(e);
		}
	}

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetProductCatalogList_005() {
        Logger.start(true,"获取商品规格列-拼团商品规格,编辑时");
        try{
            //创建一个拼团商品
            AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId1);
            setStock(addpro, 30);
            setPrice(addpro, 100d);
            String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
            SaveGrouponBean saveGrouponBean = ProductManagerCallServiceV3.initSaveGrouponBean(sellerId1,productId,80d);
            saveGrouponBean.setGrouponMemberCount(13);
            ProductManagerCallServiceV3.SaveGrouponCall(saveGrouponBean);
            
            List<GrouponProducts> gp = new YmtProductsIWapper().selectGrouponProducts(productId, null, 0);

            Integer grouponProductId = gp.get(0).getGrouponProductId();
            saveGrouponBean.setGrouponProductId(grouponProductId);

            saveGrouponBean.setGrouponCatalogs(saveGrouponBean.getGrouponCatalogs());
            saveGrouponBean.setOperateType(1);
            saveGrouponBean.setProductId(productId);
            saveGrouponBean.setSellerId(sellerId1);
            ProductManagerCallServiceV3.SaveGrouponCall(saveGrouponBean);

            getProductCatalogInfosBean.setProductId(productId);
            getProductCatalogInfosBean.setUserId(sellerId1);
            getProductCatalogInfosBean.setListType(2);
            getProductCatalogListCall.setData(getProductCatalogInfosBean);
            getProductCatalogListCall.callService();

            checkGetProductCatalogList(sellerId1,productId);
        }catch(Exception e){
            Logger.fail(e);
        }
    }
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductCatalogList_009() {
		Logger.start(true,"获取商品规格列表-新增活动库存");
		try{
			Integer sellerId=20336378;
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV2.initAddSellerSpuProduct(sellerId);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addProdReq);
			Thread.sleep(10000);

			getProductCatalogInfosBean.setProductId(productId);
			getProductCatalogInfosBean.setUserId(sellerId);
			getProductCatalogListCall.setData(getProductCatalogInfosBean);
			getProductCatalogListCall.callService();
			List<ProductCatalogDtoV2> productCatalogInfos = getProductCatalogListCall.getProductCatalogInfos();
		
			Logger.verifyEquals(productCatalogInfos.get(0).getSpuCatalogId(), 1250, "验证spu规格id");
			Logger.verifyEquals(productCatalogInfos.get(1).getSpuCatalogId(), 1249, "验证spu规格id");
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductCatalogList_010() {
		Logger.start(true,"获取商品规格列表-新增SpuCatalogId");
		try{
			Integer sellerId=20336378;
			//AddSellerProductRequest addProdReq = ProductManagerCallServiceV2.initAddSellerSpuProduct(288484);
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV2.initAddSellerSpuProduct(sellerId);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addProdReq);
			Thread.sleep(10000);

			getProductCatalogInfosBean.setProductId(productId);
			getProductCatalogInfosBean.setUserId(sellerId);
			getProductCatalogListCall.setData(getProductCatalogInfosBean);
			getProductCatalogListCall.callService();
			List<ProductCatalogDtoV2> productCatalogInfos = getProductCatalogListCall.getProductCatalogInfos();
		
			Logger.verifyEquals(productCatalogInfos.get(0).getSpuCatalogId(), 1250, "验证spu规格id");
			Logger.verifyEquals(productCatalogInfos.get(1).getSpuCatalogId(), 1249, "验证spu规格id");
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	///////////////////// 忽略case //////////////////
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_GetProductCatalogList_006() {
		Logger.start(true,"获取商品规格列表-全球优选礼包商品");
		try{
			//创建一个拼团商品
			AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId2);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			ProductManagerCallServiceV2.AddSellerGlobalPspProduct(sellerId2,productId,1);
			List<String> catalogIds = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(x->x.getsCatalogId()).collect(Collectors.toList());

			List<BigDecimal> saleCommissionList = new ArrayList<>();
			saleCommissionList.add(new BigDecimal("0.0600"));
			saleCommissionList.add(new BigDecimal("0.0700"));
			saleCommissionList.add(new BigDecimal("0.0800"));
			saleCommissionList.add(new BigDecimal("0.0900"));
			ProductManagerCallServiceV2.UpdateCatalogCommisionsV2(sellerId2,productId,catalogIds.get(0),addpro.getSellerName(),saleCommissionList);
			ProductManagerCallServiceV2.UpdateCatalogCommisionsV2(sellerId2,productId,catalogIds.get(1),addpro.getSellerName(),saleCommissionList);

			checkGetProductCatalogListV2(sellerId2,productId);
		}catch(Exception e){
			Logger.fail(e);
		}
	}


	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_GetProductCatalogList_008() {
		Logger.start(true,"获取商品规格列表-全球优选团商品");
		try{
			//创建一个拼团商品,获取商品id，设置为优选团商品，查询商品规格
			AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId2);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			ProductManagerCallServiceV2.AddSellerGlobalPspProduct(sellerId2,productId,2);
			List<String> catalogIds = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(x->x.getsCatalogId()).collect(Collectors.toList());
			//修改商品佣金
			ProductManagerCallServiceV2.UpdateCatalogCommisions(sellerId2,productId,catalogIds.get(0),addpro.getSellerName(),new BigDecimal("0.0700"));
			ProductManagerCallServiceV2.UpdateCatalogCommisions(sellerId2,productId,catalogIds.get(1),addpro.getSellerName(),new BigDecimal("0.0700"));
//			ProductManagerCallServiceV2.UpdateCatalogCommisionsV2(sellerId2,productId,catalogIds.get(0),addpro.getSellerName(),Arrays.asList(new BigDecimal("0.1000"),new BigDecimal("0.0900"),new BigDecimal("0.0800")));
			checkGetProductCatalogList(sellerId2,productId);

		}catch(Exception e){
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_GetProductCatalogList_007() {
		Logger.start(true,"获取商品规格列表-全球优选团商品");
		try{
			//创建一个拼团商品,获取商品id，设置为优选团商品，查询商品规格
			AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId2);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			ProductManagerCallServiceV2.AddSellerGlobalPspProduct(sellerId2,productId,2);
			List<String> catalogIds = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(x->x.getsCatalogId()).collect(Collectors.toList());
			//修改商品佣金
			ProductManagerCallServiceV2.UpdateCatalogCommisions(sellerId2,productId,catalogIds.get(0),addpro.getSellerName(),new BigDecimal("0.0700"));
			ProductManagerCallServiceV2.UpdateCatalogCommisions(sellerId2,productId,catalogIds.get(1),addpro.getSellerName(),new BigDecimal("0.0700"));
//			ProductManagerCallServiceV2.UpdateCatalogCommisionsV2(sellerId2,productId,catalogIds.get(0),addpro.getSellerName(),Arrays.asList(new BigDecimal("0.1000"),new BigDecimal("0.0900"),new BigDecimal("0.0800")));
			checkGetProductCatalogList(sellerId2,productId);
		}catch(Exception e){
			Logger.fail(e);
		}
	}


	private void setStock(AddSellerProductRequest addpro, Integer catalogNum) {
		addpro.getProduct().getCatalogList().forEach(catalog -> {
			catalog.setStock(catalogNum);
		});
	}

	private void setPrice(AddSellerProductRequest addpro, Double price) {
		addpro.getProduct().getCatalogList().forEach(catalog -> {
			catalog.setNewPrice(price);
			catalog.setMarketPrice(price);
			catalog.setVipPrice(price);
			catalog.setPrice(price);
		});
	}
	
	public static void checkGetProductCatalogList(int sellerId, String productId) throws Exception{      

		JSONObject ret = getProductCatalogListCall.call(sellerId, productId,0);
		System.out.println("getProductCatalogListCall:"+ret.toString());
		ProductCatalogListBean actBean = ProductManagerCallService
										.getBeanFromJson(ret.getJSONObject("Data"), 
										ProductCatalogListBean.class);
		ProductCatalogListBean tgtBean = ProductManagerCallService.getProductCatalogListBeanV2(productId);
		AssertHelper.assertResultEqual(tgtBean, actBean, "GetProductCatalogList");
	}
	
	public static void checkGetProductCatalogListV2(int sellerId, String productId) throws Exception{      

		JSONObject ret = getProductCatalogListCall.call(sellerId, productId,0);
		System.out.println("getProductCatalogListCall:"+ret.toString());
		ProductCatalogListBean actBean = ProductManagerCallService
										.getBeanFromJson(ret.getJSONObject("Data"), 
										ProductCatalogListBean.class);
		ProductCatalogListBean tgtBean = ProductManagerCallService.getProductCatalogListBeanV2(productId);
		tgtBean.getProductCatalogInfos().get(0).setSaleCommission(null);
		tgtBean.getProductCatalogInfos().get(1).setSaleCommission(null);
		actBean.getProductCatalogInfos().get(0).setSaleCommission(null);
		actBean.getProductCatalogInfos().get(1).setSaleCommission(null);
		AssertHelper.assertResultEqual(tgtBean, actBean, "GetProductCatalogList");
	}
}
